// pages/showM/showM.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    contentlist:[],
    topHeight:0,
    mtr:"",
    pages:1,
    pageNum:20,
    totalCount:0,
    hide:false,
    hideview:"main_mid_view_hide",
    loadimg:"../images/arrow_down.png",
    elementlist:[{element:"",symbol1:"",eletratio1:"",symbol2:"",eletratio2:"",             deleteimg:"../images/delete.png"}],
    whtlist:[{symbol:"",whtratio:"",deleteimg:"../images/delete.png"}],
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          topHeight:res.windowHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  getDate: function (e) {
    console.log(e.detail)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    if(that.data.contentlist.length>0){
      var count=that.data.contentlist.length;
      if(count<that.data.totalCount){//界面显示的行数 < 总行数
        if((that.data.totalCount-count)/20<1) //判断剩下的行数是否能显示20行
          that.data.pageNum=that.data.totalCount-count; //小于20行则直接显示最后的行数
        else
          that.data.pageNum=20;
      }else
        that.data.pageNum=0;//界面展示的行数>=总行数了
      
        that.data.pages+=1;
        wx.request({
          url: app.wxlocurl.url +'WXLogin/WXMtrOrderSearch' ,
          data:{mtr:that.data.mtr,ElementMod:that.data.elementlist,WhtMod:that.data.whtlist,pageNum:that.data.pageNum,pageSize:that.data.pages},
          method:"POST",
          contentType: "application/x-www-form-urlencoded;charset=utf-8",
          success: function (result) {
            // var data1 = JSON.stringify(result.data);
            // var lsdata = JSON.parse(data1);
            var data=result.data.data;
            for(var i=0;i<data.length;i++){
              that.data.contentlist.push(data[i]);
            }
            that.setData({
              contentlist:that.data.contentlist,
              totalCount:result.data.totalCount
            })
            getApp().globalData.contlist = that.data.contentlist;
          },
          fail:function(){
            wx.showToast({
              title: '失败',
              icon: 'success',
              duration: 2000
            });
          }
        })
    }
  },
  // 点击轮播图 放大预览
  handlePrevewImage(e) {
    var imgArr = e.currentTarget.dataset.list;
    var imged = [];
    var datas = this.data.contentlist;
    for (var i = 0; i < datas.length;i++){
      imged.push(app.wxImageUrl.url+datas[i].img);
    }
    wx.previewImage({
      urls: imged,
      current: imgArr,
    });

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindconfirm:function(e){
    this.setData({
      mtr:e.detail.value
    });
    this.BindWXMtrOrderSearch();
  },

  mtrInputFun:function(e){
    this.setData({
      mtr:e.detail.value
    });
  },

  BindWXMtrOrderSearch:function(){
    var that=this;
    that.data.pages=1;
    that.data.pageNum=20;
    that.data.hide=false;
    that.data.hideview="main_mid_view_hide";
    that.data.loadimg="../images/arrow_down.png";
    that.setData({
      hideview:that.data.hideview,
      loadimg:that.data.loadimg,
    })
    var phoneNumber = getApp().globalData.tel;
    wx.request({
      url: app.wxlocurl.url +'WXLogin/WXMtrOrderSearch' ,
      data: { mtr: that.data.mtr, ElementMod: that.data.elementlist, WhtMod: that.data.whtlist, pageNum: that.data.pageNum, pageSize: that.data.pages, phoneNumber:phoneNumber},
      method:"POST",
      contentType: "application/x-www-form-urlencoded;charset=utf-8",
      success: function (result) {
        var data1 = result.data.data;
        var numed;
        var deptd;
        for (var i = 0;data1.length;++i){
           numed = data1[0].Isinsider;
           deptd = data1[0].dept;
           break;
        }
        getApp().globalData.inisder = numed;
        getApp().globalData.dept = deptd;
        that.setData({

          contentlist:result.data.data,
          totalCount:result.data.totalCount,
        })
      },
      fail:function(){
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        });
      }
    })
  },

  BindLoadMore:function(e){
    var that=this;
    if(that.data.hide==false){
      that.data.hide=true;
      that.data.hideview="main_mid_view_show";
      that.data.loadimg="../images/up_arrow.png";
    }else{
      that.data.hide=false;
      that.data.hideview="main_mid_view_hide";
      that.data.loadimg="../images/arrow_down.png";
      that.data.elementlist=[{element:"",symbol1:"",eletratio1:"",symbol2:"",eletratio2:"",deleteimg:"../images/delete.png"}];
      that.data.whtlist=[{symbol:"",whtratio:"",deleteimg:"../images/delete.png"}];
    }
    that.setData({
      hideview:that.data.hideview,
      loadimg:that.data.loadimg,
      elementlist:that.data.elementlist,
      whtlist:that.data.whtlist,
    })
  },

  BindElementAdd:function(e){
    var that=this;
    var data={element:"",symbol1:"",eletratio1:"",symbol2:"",eletratio2:"",deleteimg:"../images/delete.png"};
    that.data.elementlist.push(data);
    that.setData({
      elementlist:that.data.elementlist,
    })
  },

  BindElementDel:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;//获取data-index
    that.data.elementlist.splice(index,1);
    that.setData({
      elementlist:that.data.elementlist,
    })
  },

  elementInputFun:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].element = e.detail.cls;
  },
  symbolInputFun:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].symbol1=e.detail.text;
  },
  eletratioInputFun:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].eletratio1=e.detail.value;
  },
  symbolInputTwoFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].symbol2 = e.detail.text;
  },
  eletratioInputTwoFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].eletratio2 = e.detail.value;
  },

  BindWhtAdd:function(e){
    var that=this;
    var data={symbol:"",whtratio:"",deleteimg:"../images/delete.png"};
    that.data.whtlist.push(data);
    that.setData({
      whtlist:that.data.whtlist,
    })
  },
  BindWhtDel:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;//获取data-index
    that.data.whtlist.splice(index,1);
    that.setData({
      whtlist:that.data.whtlist,
    })
  },
  whtsymbolInputFun:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    that.data.whtlist[index].symbol=e.detail.value;
  },
  whtratioInputFun:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    that.data.whtlist[index].eletratio=e.detail.value;
  },
  // 点击 加入购物车
  handleCartAdd(e) {
    var DataGood = e.currentTarget.dataset.index;
    // 1 获取缓存中的购物车 数组
    let cart = wx.getStorageSync("cart") || [];
    // 2 判断 商品对象是否存在于购物车数组中
    let index = cart.findIndex(v => v.ID === DataGood.ID);
    if (index === -1) {
      //3  不存在 第一次添加
      DataGood.num = 1;
      DataGood.checkeds = true;
      cart.push(DataGood);
    } else {
      // 4 已经存在购物车数据 执行 num++
      cart[index].num++;
    }
    // 5 把购物车重新添加回缓存中
    wx.setStorageSync("cart", cart);
    // 6 弹窗提示
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      // true 防止用户 手抖 疯狂点击按钮 
      mask: true
    });
  },

})